/*
 * eds.h - EnCluster Base DB Server part types and definitions
 * Clunix, yjlee@clunix.com, glory@clunix.com, 2001.7.19.
 * Clunix, cezanne@clunix.com, 2005.2
 *
 * Copyright (c) 2001-2005 by Clunix Inc. All Rights Reserved.
 */

#ifndef _EDS_H_
#define _EDS_H_

#include "ecm.h"
#include "sr.h"
#include "ebc.h"
#include "eds_types.h"
#include "ecx.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {
	EDS_OK = 0,
	ERR_EDS_GENERAL,
	ERR_EDS_NOTEXIST,
	ERR_EDS_WRONG_SNAPSHOT,
	ERR_EDS_EXIST,
	ERR_EDS_CTXROW,
	ERR_EDS_CTXCOL,
	ERR_EDS_CTXXEONLY,
	ERR_EDS_CTXHMSONLY,
	ERR_EDS_CTXENUMONLY,
	ERR_EDS_NOPROCESS,
	ERR_EDS_NOBEGIN,
	ERR_EDS_SUPER_SRT
} eds_err_t;

typedef enum {
	EDBD_MODE_SINGLE,
	EDBD_MODE_MULTI,
} edbd_mode_t;

typedef ecm_uint64	xid_t;
typedef ecm_uint32	eds_perm_t;
typedef ecm_uint32	eds_fid_t;
typedef ecm_uint32	eds_sigid_t;

#define EDS_INVALID_FID	0
#define EDS_IS_INVALID_FID(fid)	(fid == 0)

typedef sr_t *(*edfunc_t)(const sr_t *);

typedef int	func_id_t;

typedef struct {
	char		*modname;
	void		*handle;
	char		*smod_path;
	char		*so_path;
	struct list_head	list;
#ifdef ECM_DEBUG
	char		*dbgset;
#endif
} eds_mod_t;

typedef struct {
	sr_t *(*edfunc)(const sr_t *);
	int	nargs;
	srt_t	*srt_args;
	srt_t	srt_ret;
	ECMBOOL	autonomous;
	sr_t	*sr_edexpr;
	eds_mod_t	*mod;
} funcsig_t;

typedef struct {
	const char	*name;
	eds_fid_t	fid;
	ecm_uint32	nsigs;
	funcsig_t	*sigs;
} funcdef_t;

extern sr_t	*sr_eds_conf, *sr_eds_root, *sr_eds_xe_forms;
extern sr_t	*sr_eds_CSSs, *sr_eds_scripts, *sr_eds_banner_img;

extern struct list_head	eds_modhead;

ECMBOOL eds_read_enc_sr(int fd, sr_t **psr_res, ecm_size_t bufsize);
ECMBOOL eds_write_enc_sr(int fd, sr_t *sr_enc, ecm_size_t bufsize);
ECMBOOL eds_read_sr_conf(const char *confpath, sr_t **psr_conf);
ECMBOOL eds_write_sr_conf(const char *confpath, sr_t *sr_conf);
eds_err_t eds_read_snapshot(const char *snapshot_fname);
eds_err_t eds_take_snapshot(const char *snapshot_fname);
ECMBOOL eds_create_snapshot(void);

void eds_build_anchor_stage(sr_t *, sr_t *);

void eds_validate_srx(sr_t *sr);
ecm_uint32 eds_get_attr_update_self(const sr_t *sr);
ecm_uint32 eds_get_attr_update(const sr_t *sr);
ECMBOOL eds_set_attr_update(sr_t *sr, ecm_uint32 attr_update);

ecm_uint32 eds_get_attr_fetch_self(const sr_t *sr);
ecm_uint32 eds_get_attr_fetch(const sr_t *sr);
ECMBOOL eds_set_attr_fetch(sr_t *sr, ecm_uint32 attr_fetch);
ecm_uint32 eds_get_attr_system(const sr_t *sr);

ecm_uint32 eds_get_attr_system_self(const sr_t *sr);
ECMBOOL eds_set_attr_system(sr_t *sr, ecm_uint32 attr_system);
void eds_add_attr_system(sr_t *sr, ecm_uint32 attr_system);
void eds_del_attr_system(sr_t *sr, ecm_uint32 attr_system);

xid_t eds_get_owner(const sr_t *sr);
xid_t eds_get_owner_self(const sr_t *sr);
ECMBOOL eds_set_owner(sr_t *sr, xid_t owner);

const char *eds_get_user(const sr_t *sr);
const char *eds_get_user_self(const sr_t *sr);
ECMBOOL eds_set_user(sr_t *sr, const char *username);

const char *eds_get_group(const sr_t *sr);
const char *eds_get_group_self(const sr_t *sr);
ECMBOOL eds_set_group(sr_t *sr, const char *grpname);

eds_perm_t eds_get_perm(const sr_t *sr);
eds_perm_t eds_get_perm_self(const sr_t *sr);
ECMBOOL eds_set_perm(sr_t *sr, eds_perm_t gid);

sr_t *eds_get_reactors(sr_t *sr);
ECMBOOL eds_set_reactors(sr_t *sr, const sr_t *sr_reactors);
ECMBOOL eds_add_reactors(sr_t *sr, const sr_t *csr_edexprs);

ecm_uint32 eds_get_flags(const sr_t *sr);
void eds_set_flags(sr_t *sr, ecm_uint32 flags);
void eds_add_flags(sr_t *sr, ecm_uint32 flags);
void eds_del_flags(sr_t *sr, ecm_uint32 flags);

ECMBOOL eds_initialize(void);
ECMBOOL eds_add_module_types(const char *clxdir, const char *modname);

ECMBOOL eds_types_add(sr_t *sr_smod_descs, void *handle);
void eds_types_remove(sr_t *sr_smod_descs);
eds_err_t eds_type_add(sr_t *sr_type_info, void *handle);
ECMBOOL eds_exec_initializer(void *handle);
void eds_exec_finalizer(void *handle);
eds_mod_t *eds_find_module(const char *modname);
eds_mod_t *eds_load_module(const char *modname);
void eds_unload_module(const char *modname);

ECMBOOL eds_functions_add(const sr_t *sr_funcdefs, eds_mod_t *mod);
void eds_functions_remove(sr_t *sr_funcdefs);

eds_fid_t eds_get_userfunc_fid(const char *funcname);
funcdef_t *eds_fid_to_funcdef(eds_fid_t fid);

typedef enum {
	EDS_CLSATTR_NONE,
	EDS_CLSATTR_XEONLY,
	EDS_CLSATTR_HMSONLY,
	EDS_CLSATTR_ENUMONLY
} eds_clsattr_t;

typedef struct {
	ecm_uint32	priority;
	const char	*clsname;
	eds_mod_t	*mod;
	ECMBOOL		supplementary, HMS;
	ecx_ctxcls_begin_t	func_begin;
	ecx_ctxcls_proc_t	func_proc;
	ecx_ctxcls_end_t	func_end;
	eds_clsattr_t	attr;
	ecm_uint32	row;
	char		*content_type;
	sr_t		*sr_colspec;
} eds_ctxcls_t;

typedef struct {
	const char	*ctxname;
	srt_t		srt;
	eds_ctxcls_t	*ctxcls;
	sr_t		*sr_ctx_val;
	sr_t		*sr_edexpr;
} eds_ctx_t;

eds_err_t eds_add_UI(eds_mod_t *mod, const char *modname, sr_t *sr_UI);
void eds_del_UI(sr_t *sr_UI);

eds_ctxcls_t *eds_get_ctxcls(const char *clsname);
eds_err_t eds_add_ctxcls(eds_mod_t *mod, sr_t *sr_ctxcls, void *handle);
void eds_del_ctxcls(const char *clsname);

eds_err_t eds_add_ctx(const sr_t *sr_ctx);
void eds_del_ctx(sr_t *sr_ctx);
eds_ctx_t *eds_get_ctx(srt_t srt, const char *ctxname);
eds_ctx_t *eds_get_ctx_of_cls(srt_t srt, const char *clsname);

eds_err_t eds_add_embed(const char *modname, sr_t *sr_embed);
void eds_del_embed(sr_t *sr_embed);
const char *eds_get_embed(const char *embed_id);

void eds_add_menu(const char *modname, sr_t *sr_MENU);
void eds_del_menu(const char *modname);
sr_t *eds_get_menus(void);

void eds_add_active_area(const char *modname, sr_t *sr_area_info);
sr_t *eds_get_active_areas(void);

sr_t *eds_xecreate(sr_t *sr_HMS, const char *name, srt_t srt, ECMBOOL isolated);
ECM_INLINE sr_t *eds_get_xe_form(srt_t srt);
void eds_add_xe_forms(sr_t *sr_forms);
void eds_del_xe_forms(sr_t *sr_forms);

sr_t *eds_get_xe_forms(void);
ECMBOOL eds_is_xe_srt(srt_t srt);
ECMBOOL eds_is_xe_sr(const sr_t *sr);
sr_t *eds_get_xe_form(srt_t srt);
srt_t eds_get_srt(srt_t srt, const sr_t *sr_LNP);
sr_t *eds_xe_create(srt_t srt);

char *eds_build_xe_name(const char *name, ECMBOOL *panony);

typedef struct {
	void (*ecx_error)(ecx_err_t err, const char *msg);
	ECMBOOL (*ecx_process_entity)(const sr_t *, const sr_t *);
	sr_t *(*ecx_get_self)(void);
	const char *(*ecx_get_stage)(ecm_uint32);
	sr_t *(*ecx_get_parent)(sr_t *);
	sr_t *(*ecx_get_ctxval)(ecm_uint32, ecm_uint32);
	ecm_uint32 (*ecx_get_ctxval_rowcount)(void);
	void (*ecx_printf)(const char *, va_list);
	void (*ecx_printf_raw)(const char *, va_list);
	const char *(*ecx_username)(void);
	const char *(*ecx_sessionid)(void);
	const sr_t *(*ecx_arg)(void);
	ECMBOOL (*ecx_is_editing)(void);
	ECMBOOL (*ecx_is_applying)(void);
	ECMBOOL (*ecx_is_showing)(void);

	void (*ecx_reg_private)(ecm_cptr_t);
	ecm_cptr_t (*ecx_get_private)(void);

	const char *(*ecx_parent_ctxcls)(void);
	ECMBOOL (*ecx_has_supplementary)(const char *clsname);

	ECXATTR_t (*ecx_attr)(ECXATTR_t, va_list);
	void (*ecx_add_attr)(ECXDOM_t, const char *, const char *);
	void (*ecx_set_attr)(ECXDOM_t, ECXATTR_t);

	ECMBOOL (*ecx_set_content)(const char *path);

	char *(*ecx_ESTAGE)(const sr_t *);
	char *(*ecx_EARG)(const sr_t *);
	const char *(*ecx_DOM_ID)(ECXDOM_t);

	ECXDOM_t (*ecx_FORM_BEGIN)(void);
	void (*ecx_FORM_END)(void);
	void (*ecx_FORM_BUTTON)(const char *, ecx_formbutton_t);
	void (*ecx_ACTION_BUTTON)(const char *, const char *, ECXDOM_t, const char *);
	void (*ecx_NOFORM_BEGIN)(void);
	void (*ecx_NOFORM_END)(void);

	ECXDOM_t (*ecx_TEXT)(const char *, const char *text);
	void (*ecx_POPUP_BEGIN)(sr_t *sr, const char *, int, int);
	void (*ecx_POPUP_END)(void);
	void (*ecx_POPUP_FORM_BUTTON)(const char *, ecx_formbutton_t, char *);
	ECXDOM_t (*ecx_GROUP_BEGIN)(const char *, const char *, const char *);
	void (*ecx_GROUP_END)(void);
	ECXDOM_t (*ecx_FIELD)(const char *, const char *, const char *, const char *, va_list, const sr_t *);
	ECXDOM_t (*ecx_TEXTAREA)(const char *, const char *);
	ECXDOM_t (*ecx_PASSWORD)(const char *, const char *, const char *);
	ECXDOM_t (*ecx_BUTTON)(const char *, const char *, ECXATTR_t);
	ECXDOM_t (*ecx_RADIO)(const char *, const char *fmt, va_list ap, ECXATTR_t);
	ECXDOM_t (*ecx_CHECKBOX)(const char *, const char *fmt, va_list ap, ECXATTR_t);
	void (*ecx_BR)(void);
	ECXDOM_t (*ecx_NOBR_BEGIN)(void);
	void (*ecx_NOBR_END)(void);
	ECXDOM_t (*ecx_SPAN_BEGIN)(void);
	void (*ecx_SPAN_END)(void);
	ECXDOM_t (*ecx_IFRAME)(const char *, int, ECXATTR_t);
	ECXDOM_t (*ecx_HIDDEN)(const char *, const char *, va_list);
	ECXDOM_t (*ecx_UPLOAD)(const char *, const char *);
	ECXDOM_t (*ecx_FILE_BROWSE)(const char *, const char *);
	ECMBOOL (*ecx_DOWNLOAD_BEGIN)(ebc_ndid_t ndid, const char *, const char *);
	void (*ecx_DOWNLOAD_END)(void);
	ECMBOOL (*ecx_DOWNLOAD)(ebc_ndid_t ndid, const char *);
	ECXDOM_t (*ecx_OBJECT_BEGIN)(const char *, const char *, const char *);
	void (*ecx_OBJECT_END)(void);
	ECXDOM_t (*ecx_PARAM)(const char *, const char *);
	const sr_t *(*ecx_get_GROUP)(const char *);
	const char *(*ecx_get_FIELD)(const char *, va_list);
	const char *(*ecx_get_PASSWORD)(const char *);
	const char *(*ecx_get_RADIO)(const char *, va_list);
	const sr_t *(*ecx_get_CHECKBOX)(const char *, va_list);
	const sr_t *(*ecx_get_SELECT)(const char *name);
	const char *(*ecx_get_UPLOAD)(const char *);
	const char *(*ecx_get_FILE_BROWSE)(const char *);
	const char *(*ecx_get_user)(void);
	ECXDOM_t (*ecx_SELECT_BEGIN)(const char *, ECMBOOL, ECMBOOL);
	void (*ecx_SELECT_END)(void);
	ECXDOM_t (*ecx_OPTION)(ECMBOOL selected, const char *);
	ECXDOM_t (*ecx_IMG)(const char *, const char *, const sr_t *sr_stage);
	void (*ecx_IMGSRC)(const ecm_uint8 *, ecm_size_t);

	ECXDOM_t (*ecx_TABLE_BEGIN)(ECXATTR_t);
	void (*ecx_TABLE_END)(void);
	ECXDOM_t (*ecx_TR)(ECXATTR_t);
	ECXDOM_t (*ecx_TD)(ECXATTR_t);
	ECXDOM_t (*ecx_DIV_BEGIN)(void);
	void (*ecx_DIV_END)(void);

	void (*ecx_SANCHOR)(ecx_sanchor_mode_t, const sr_t *, const sr_t *);
	ECXDOM_t (*ecx_SANCHOR_BEGIN)(ecx_sanchor_mode_t, const char *,
				      const sr_t *, const char *);
	void (*ecx_SANCHOR_END)(void);
	ECMBOOL (*ecx_BACK)(ECMBOOL, ecm_uint32);
	void (*ecx_DYNAMIC_BEGIN)(ECMBOOL enabled);
	void (*ecx_DYNAMIC_END)(void);
} eds_ecx_funcs_t;

extern eds_ecx_funcs_t	*eds_ecx_funcs;

#ifdef ECM_DEBUG

#ifdef ECM_MTRACE

extern sr_t *get_sr_mtraces(void);

#endif

extern void (*eds_set_dbgset)(const char *dbgset);
extern void (*eds_debug)(const char *dbgset, const char *file, int line,
			 const char *fmt, va_list ap);

#endif

#ifdef __cplusplus
}
#endif

#endif		/* _EDS_H_ */
